package com.tanhua.dubbo.api;

import com.tanhua.domain.db.Question;
import com.tanhua.domain.db.SoulOption;
import com.tanhua.domain.db.SoulQuestion;
import com.tanhua.domain.db.SoulQuestions;
import com.tanhua.domain.vo.PageResult;
import com.tanhua.domain.vo.QuestionVo;
import com.tanhua.domain.vo.SoulVo;
import org.apache.dubbo.config.annotation.Service;
import org.bson.types.ObjectId;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;

import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service
public class BackApiImpl implements BackApi {
    @Autowired
    private MongoTemplate mongoTemplate;


    @Override
    public PageResult<SoulQuestion> findList(int page, int pagesize) {
        //查询题目库
        Query query = new Query();
        query.with(PageRequest.of(page - 1, pagesize));
        List<SoulQuestion> questionList = mongoTemplate.find(query, SoulQuestion.class);
        long count = mongoTemplate.count(query, Question.class);

        //封装返回值
        List<SoulVo> voList = new ArrayList<>();

        //查询 等级库 封装返回值
        for (SoulQuestion question : questionList) {
            SoulVo vo = new SoulVo();
            BeanUtils.copyProperties(question, vo);
            ObjectId id = new ObjectId(question.getQuestionsId());
            Query query1 = new Query(Criteria.where("id").is(id));
            SoulQuestions soulQuestions = mongoTemplate.findOne(query1, SoulQuestions.class);
            vo.setQuestionName(soulQuestions.getName());

            voList.add(vo);
        }

        int pages = (int) Math.ceil(count * 1.0 / pagesize);
        PageResult pageResult = new PageResult(
                (int) count,
                pagesize,
                pages,
                page,
                voList);
        return pageResult;
    }

    @Override
    public QuestionVo findById(String backId) {
        QuestionVo vo = new QuestionVo();

        //查询 试卷库
        ObjectId objectId = new ObjectId(backId);
        /*Query query = new Query(Criteria.where("id").is(objectId));*/

        //获得题目
        SoulQuestion question = mongoTemplate.findById(objectId, SoulQuestion.class);

        //通过题目查询选项表
        List<String> list = new ArrayList<>();
        String[] op = question.getOptions().split(",");
        for (String option : op) {
            ObjectId objectId2 = new ObjectId(option);
            SoulOption options = mongoTemplate.findById(objectId2, SoulOption.class);
            list.add(options.getOption());
        }

        //通过题目表等级id查 等级表
        ObjectId objectId3 = new ObjectId(question.getQuestionsId());
        SoulQuestions questions = mongoTemplate.findById(objectId3, SoulQuestions.class);

        vo.setOptions(list);
        vo.setQuestion(question.getQuestion());
        vo.setQuestions(questions.getName());

        return vo;
    }

    //todo 待修改
    @Override
    public void update(String id, String question, String questions, String[] options) {

    }

    @Override
    //todo 待增加
    public void insert(String question, String questions, String[] options) {
        SoulOption soulOption = new SoulOption();
        soulOption.setOption(Arrays.toString(options));
/*
        mongoTemplate.insert()
*/
    }

    @Override
    public void deleteSoul(String backId) {
        //通过题目获得选项Id
        //查询 试卷库
        ObjectId objectId = new ObjectId(backId);
        /*Query query = new Query(Criteria.where("id").is(objectId));*/

        //获得题目
        SoulQuestion question = mongoTemplate.findById(objectId, SoulQuestion.class);


        Query query = new Query(Criteria.where("id").is(objectId));
        mongoTemplate.remove(query, SoulQuestion.class);

        //通过题目查询选项表
        List<String> list = new ArrayList<>();
        String[] op = question.getOptions().split(",");
        for (String option : op) {
            ObjectId objectId2 = new ObjectId(option);
            Query q = new Query(Criteria.where("id").is(objectId2));
            mongoTemplate.remove(q, SoulOption.class);
        }

    }

}
